package com.ciwong.epaper.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.bean.RfTokenInfo;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.util.EJumpManager;
import com.ciwong.epaper.util.FeedbackUtil;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.utils.BaseConstants;

import java.io.IOException;

/**
 * 个人信息保护指引页面
 * Created by paul on 2023/12/06.
 */
public class FirstAgreementActivity extends Activity {
    private static final String TAG = "FirstAgreementActivity";
    private TextView tx_use_url, tx_secret_url, tx_child_url;
    private Button btn_go_black, btn_go_main;
    private String packageName = "";
    private BaseRequest.VerifyInfo verifyInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_agreement_layout);
        findViews();
        initEvent();
        init();
    }

    private void findViews() {
        tx_use_url = findViewById(R.id.tx_use_url);
        tx_secret_url = findViewById(R.id.tx_secret_url);
        tx_child_url = findViewById(R.id.tx_child_url);
        btn_go_black = findViewById(R.id.btn_go_black);
        btn_go_main = findViewById(R.id.btn_go_main);
    }

    private void initEvent() {
        tx_use_url.setMovementMethod(LinkMovementMethod.getInstance());
        tx_secret_url.setMovementMethod(LinkMovementMethod.getInstance());
        tx_child_url.setMovementMethod(LinkMovementMethod.getInstance());

        btn_go_black.setOnClickListener(myOnClickListener);
        btn_go_main.setOnClickListener(myOnClickListener);
    }

    private void init() {
        packageName = getPackageName();
    }

    /**
     * 点击按钮
     *
     * @author res-jxliu
     */
    private ViewOnClickListener myOnClickListener = new ViewOnClickListener() {
        @Override
        public void avertRepeatOnClick(View v) {
            int vId = v.getId();
            if (vId == R.id.btn_go_black) {
                finish();
            } else if (vId == R.id.btn_go_main) {
                try {
                    CWSys.setSharedBoolean("isFirstUse" + packageName, false);//以后都不再是第一次使用
                    CWSys.setSharedBoolean("agreement" + packageName, true);//以后都不再显示同意按钮
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //跳转新手引导
                //EJumpManager.jumpToGuide(0, AgreementActivity.this);
                //跳转到主界面
                checkToken();
            }
        }
    };
    private void checkToken() {
        try {
            verifyInfo = HttpRequest.getVerifyInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != verifyInfo) {
            String accessToken = verifyInfo.getAccessToken();
            String refreshToken = verifyInfo.getRefreshToken();
            if (!TextUtils.isEmpty(accessToken) && !TextUtils.isEmpty(refreshToken)) {
                updateUserToken(refreshToken);
            } else {
                goToMainActivity(false);
            }
        } else {
            goToMainActivity(false);
        }
    }
    private void goToMainActivity(boolean isLogin) {
        //是否同意隐私协议
        boolean privacyAgreement = false;
        try {
            privacyAgreement = CWSys.getSharedBoolean("privacyAgreement" + packageName + getPackageManager()
                    .getPackageInfo(getPackageName(), 0).versionName, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isLogin && privacyAgreement) {
            //用户已经登录
            EJumpManager.jumpToMain(0, this);
        } else {
            //用户未登录
            EJumpManager.jumpToLogin(0, this);
        }
        finish();
    }
    private void updateUserToken(String rfToken) {
        MeDao.getInstance().updateUserToken(rfToken, new BaseCallBack() {
            @Override
            public void success(Object data) {
                RfTokenInfo rfTokenInfo = (RfTokenInfo) data;
                String accessToken = rfTokenInfo.getAccessToken();
                String refreshToken = rfTokenInfo.getRefreshToken();
                String expires = rfTokenInfo.getRefreshToken();
                if (null != verifyInfo) {
                    verifyInfo.setAccessToken(accessToken);
                    verifyInfo.setRefreshToken(refreshToken);
                    verifyInfo.setExpiresIn(expires);
                    HttpRequest.setVerifyInfo(verifyInfo);
                    try {
                        CWSys.setSharedSerializable(BaseConstants.SHARE_KEY_VERITIFY_INFO,
                                verifyInfo);
                        //上报登录信息
                        FeedbackUtil.getInstance().asyncUploadUserLogin();
                        goToMainActivity(true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    goToMainActivity(false);
                }
            }

            @Override
            public void failed(int errorCode, Object data) {
                goToMainActivity(false);
            }

            @Override
            public void failed(Object data) {
                goToMainActivity(true);
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
    }
}
